package off_apportionment

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	// "bp/internal/util"
	"context"
)

// DisableOffApportionmentsByOutTransNo 根据外部交易号作废优惠交易明细
func (s *OffApportionmentEntityServiceImp) DisableOffApportionmentsByOutTransNo(ctx context.Context, q *sqlc.Queries, outTransNo string) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffApportionmentsByOutTransNo(ctx, outTransNo)
	if err != nil {
		return
	}
	return
}

type DisableOffApportionmentsByOutTransNoAndOutOrderNoParam struct {
	OutTransNo string
	OutOrderNo string
}

// DisableOffApportionmentsByOutTransNoAndOutOrderNo 根据外部交易号+外部订单号作废优惠交易明细
func (s *OffApportionmentEntityServiceImp) DisableOffApportionmentsByOutTransNoAndOutOrderNo(ctx context.Context, q *sqlc.Queries, ptr *DisableOffApportionmentsByOutTransNoAndOutOrderNoParam) (err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffApportionmentsByOutTransNoAndOutOrderNo(ctx, sqlc.DisableOffApportionmentsByOutTransNoAndOutOrderNoParams{
		OutTransNo: ptr.OutTransNo,
		OutOrderNo: ptr.OutOrderNo,
	})
	if err != nil {
		return
	}
	return
}

type RefreshOffApportionmentsByTransactionIDParam struct {
	TransactionID                    int64
	UpsertOffApportionmentParamsPtrs []*sqlc.UpsertOffApportionmentParams
}

// RefreshOffApportionmentsByTransactionID 根据交易id刷新优惠分摊
func (s *OffApportionmentEntityServiceImp) RefreshOffApportionmentsByTransactionID(ctx context.Context, q *sqlc.Queries, ptr *RefreshOffApportionmentsByTransactionIDParam) (offApportionmentPtrs []*sqlc.OffApportionment, err error) {
	// slog := util.NewContextLogger(ctx, s.slog)
	err = q.DisableOffApportionmentsByTransactionID(ctx, ptr.TransactionID)
	if err != nil {
		return
	}
	for _, ptr := range ptr.UpsertOffApportionmentParamsPtrs {
		offApportionment, upErr := q.UpsertOffApportionment(ctx, *ptr)
		if upErr != nil {
			err = upErr
			return
		}
		offApportionmentPtrs = append(offApportionmentPtrs, &offApportionment)
	}
	return
}
